package com.detrum.sportcamera.jlibrtp;

public class DFrame {
    public final int HeaderLen = 8;


    static long calculateChecksum(byte[] buf, int len) {
        int length = len;
        int i = 0;

        long sum = 0;
        long data ;

        // Handle all pairs
        while (length > 1) {
            // Corrected to include @Andy's edits and various comments on Stack Overflow
		    data = (((buf[i] << 8) & 0xFF00) | ((buf[i + 1]) & 0xFF));
            sum += data;
            // 1's complement carry bit correction in 16-bits (detecting sign extension)
            if ((sum & 0xFFFF0000) > 0) {
                sum = sum & 0xFFFF;
                sum += 1;
            }

            i += 2;
            length -= 2;
       }

       // Handle remaining byte in odd length buffers
       if (length > 0) {
           // Corrected to include @Andy's edits and various comments on Stack Overflow
           sum += (buf[i] << 8 & 0xFF00);
           // 1's complement carry bit correction in 16-bits (detecting sign extension)
           if ((sum & 0xFFFF0000) > 0) {
               sum = sum & 0xFFFF;
               sum += 1;
           }
       }

       // Final 1's complement value correction to 16-bits
       sum = ~sum;
       sum = sum & 0xFFFF;
       return sum;
    }

    public DFrame(byte[] aRawPkt, int packetSize){
        int remOct = packetSize - HeaderLen;
        int payload_len = 0;
        long checkcum = 0;

        if(remOct > 0)
        {
            payload_len = ((aRawPkt[4] << 8) & 0xFF00) | (aRawPkt[5] & 0xFF);
            if( payload_len != remOct )
            {
                return ;
            }

            checkcum = calculateChecksum(aRawPkt,packetSize);
            if(checkcum !=0 )
            {
                return ;
            }
        }
    }
}
